package com.pawelsekara.curves;

import org.andengine.engine.camera.ZoomCamera;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.AutoParallaxBackground;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.TextureRegion;

import android.util.Log;

public class LevelSelectScene extends Scene implements IOnSceneTouchListener {

	private static LevelSelectScene mInstance;
	private float ratio;
	private TextureRegion levelSelectBG;
	private ZoomCamera mCamera;
	private float x;
	private float y;
	private float offsetX;
	private float offsetY;
	private Sprite BG;
	private AutoParallaxBackground mBG;

	private LevelSelectScene() {
		super();
		initLevelSelectScene();

	}

	public static LevelSelectScene getInstance() {
		if (mInstance == null) {
			return mInstance = new LevelSelectScene();
		}
		return mInstance;
	}

	private void initLevelSelectScene() {
		mCamera = SceneManager.curvesgame.getCamera();
		setBackground(new Background(1, 1, 1));
		mBG = new AutoParallaxBackground(0, 0, 0, 0);
		mBG.attachParallaxEntity(new ParallaxEntity(0, new Sprite(0, 0,
				levelSelectBG.getWidth() * ratio, levelSelectBG.getHeight()
						* ratio, levelSelectBG, SceneManager.curvesgame
						.getVertexBufferObjectManager())));
		setBackground(mBG);
		levelSelectBG = SceneManager.curvesgame.levelMap.get("levelselectbg");
		ratio = CurvesGame.CAMERA_WIDTH / levelSelectBG.getWidth();

		BG = new Sprite(0, 0, levelSelectBG.getWidth() * ratio,
				levelSelectBG.getHeight() * ratio, levelSelectBG,
				SceneManager.curvesgame.getVertexBufferObjectManager());
		BG.setPosition(0, CurvesGame.CAMERA_HEIGHT / 2 - BG.getHeightScaled()
				/ 2);

	//	attachChild(BG);
		setOnSceneTouchListener(this);
		// registerTouchArea(BG);

	}

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		switch (pSceneTouchEvent.getAction()) {
		case TouchEvent.ACTION_DOWN:
			x = pSceneTouchEvent.getX();
			y = pSceneTouchEvent.getY();
			Log.i("act", "ACTION_DOWN");
			return true;
		case TouchEvent.ACTION_MOVE:
			offsetX = pSceneTouchEvent.getX() - x;
			offsetY = pSceneTouchEvent.getY() - y;
			// BG.getTextureRegion().setTextureX(BG.getTextureRegion().getTextureX()
			// - offsetX);
			mCamera.setCenter(mCamera.getCenterX() - offsetX,
					mCamera.getCenterY());
			Log.i("act", "ACTION_MOVE - " + offsetX + " ; " + offsetY);
			return true;
		default:
			return false;
		}
	}

}
